package com.choichumgming.rentcat.web.controller;


import com.choichumgming.rentcat.common.api.CommonResult;
import com.choichumgming.rentcat.model.UmsMemberReceiveAddress;
import com.choichumgming.rentcat.service.UmsMemberReceiveAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员收货地址管理Controller
 */
@RestController
@Api(tags = "UmsMemberReceiveAddressController", description = "会员收货地址管理")
@RequestMapping("/address")
public class UmsMemberReceiveAddressController {
    @Autowired
    private UmsMemberReceiveAddressService memberReceiveAddressService;

    @ApiOperation("添加收货地址")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@RequestBody UmsMemberReceiveAddress address) {
        int count = memberReceiveAddressService.add(address);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除收货地址")
    @RequestMapping(value = "/delete/{addressId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult delete(@PathVariable("addressId") Long addressId) {
        int count = memberReceiveAddressService.delete(addressId);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改收货地址")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody UmsMemberReceiveAddress address) {
        int count = memberReceiveAddressService.update(address);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("根据用户Id显示所有收货地址")
    @RequestMapping(value = "/list/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<UmsMemberReceiveAddress>> getAddressList(@PathVariable("userId") Long userId) {
        List<UmsMemberReceiveAddress> addressList = memberReceiveAddressService.list(userId);
        return CommonResult.success(addressList);
    }

    @ApiOperation("显示收货地址")
    @RequestMapping(value = "/item/{addressId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsMemberReceiveAddress> getItem(@PathVariable("addressId") Long addressId) {
        UmsMemberReceiveAddress address = memberReceiveAddressService.getItem(addressId);
        return CommonResult.success(address);
    }
}
